using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpellManager {
	
	static string typeName = typeof(Spell).ToString();
	
	static GenericManager<Spell> _m;
	
	static GenericManager<Spell> m
	{
		get
		{
			if(_m == null)
				_m = new GenericManager<Spell>(typeName);
			return _m;
		}
	}
	
	public static void resetManager()
	{
		_m = new GenericManager<Spell>(typeName);
	}
	
	public static Dictionary<string,string> index
	{
		get
		{
			return m.index;
		}
	}
	
	static Dictionary<string,int> _spellLists;
	
	public static Dictionary<string,int> spellLists
	{
		get
		{
			if(_spellLists == null)
			{
				//Debug.Log("Woot?");
				object temp = m.LoadExtraFile("spellLists");
				//Debug.Log(temp);
				Dictionary<string,object> tempList = temp as Dictionary<string,object>;
				//Debug.Log("Woozy");
				_spellLists = new Dictionary<string,int>();
				
				if(tempList != null)
				{
					foreach(var i in tempList)
					{
						_spellLists.Add(i.Key as string, int.Parse(i.Value as string));
					}
				}
			}
			return _spellLists;
		}
	}
	
	static Dictionary<string,string> _spellsByListAndLevel;
	public static Dictionary<string,string> spellsByListAndLevel
	{
		get
		{
			if(_spellsByListAndLevel == null)
			{
				object temp = m.LoadExtraFile("spellsByListAndLevel");
				
				Dictionary<string,object> tempList = temp as Dictionary<string,object>;
				
				_spellsByListAndLevel = new Dictionary<string, string>();
				
				if(tempList != null)
				{
					foreach(var i in tempList)
					{
						_spellsByListAndLevel.Add(i.Key as string, i.Value as string);
					}
				}
			}
			return _spellsByListAndLevel;
		}
	}
	
	public static Spell Load(string name)
	{
		return m.LoadObject(name);
	}
	
	public static bool isCustom(string name)
	{
		return m.isCustom(name);
	}

	public static Spell AddSpell(	string name, string school, string castingTime, string components, string range, string durations, 
									string savingThrough, bool spellResistance, List<string> spellList, List<int> spellLevel, 
									string description = "", bool custom = false)
	{
		Spell a = new Spell();
		a.name = name;
		a.description = description;
		a.spellLevel = spellLevel;
		a.spellList = spellList;
		
		a.school = school;
		a.castingTime = castingTime;
		a.components = components;
		a.range = range;
		a.duration = durations;
		a.save = savingThrough;
		a.spellResistance = spellResistance;

		foreach(string list in a.spellList)
		{
			if(!spellLists.ContainsKey(list))
			{
				spellLists.Add(list, 1);
			}
			else
				spellLists[list]++;
		}
		
		a = m.AddObject(a,custom);
		
		m.WriteIntoExtraFile("spellLists", spellLists);
		
		Dictionary<string, string> sbll = spellsByListAndLevel;
		sbll.Add(a.fileName, "");
		
		for(int i = 0; i < a.spellList.Count; i++)
		{
			sbll[a.fileName] += a.spellList[i] + "_" + a.spellLevel[i] + "_";
		}
		
		m.WriteIntoExtraFile("spellsByListAndLevel", sbll);
		
		return a;
	}
	
	public static bool SaveSpell(Spell a, string file)
	{
		Spell s = Load(file);
		
		a.fileName = file;
		
		if(s != null && s.spellList != null)
		{
			foreach(string list in s.spellList)
			{
				if(spellLists.ContainsKey(list))
				{
					spellLists[list]--;
					if(spellLists[list] <= 0)
						spellLists.Remove(list);
				}
			}
			
			m.WriteIntoExtraFile("spellLists", spellLists);
		}
		
		foreach(string list in a.spellList)
		{
			if(!spellLists.ContainsKey(list))
			{
				spellLists.Add(list, 1);
			}
			else
				spellLists[list]++;
		}
		
		m.WriteIntoExtraFile("spellLists", spellLists);
		
		Dictionary<string, string> sbll = spellsByListAndLevel;
		sbll[a.fileName] = "";
		
		for(int i = 0; i < a.spellList.Count; i++)
		{
			sbll[a.fileName] += a.spellList[i] + "_" + a.spellLevel[i] + "_";
		}
		
		m.WriteIntoExtraFile("spellsByListAndLevel", sbll);
		
		return m.SaveObject(a, file);
	}
	
	public static void DeleteSpell(string name)
	{
		Spell s = Load(name);
		
		if(s != null && s.spellList != null)
		{
			foreach(string list in s.spellList)
			{
				if(spellLists.ContainsKey(list))
				{
					spellLists[list]--;
					if(spellLists[list] <= 0)
						spellLists.Remove(list);
				}
			}
			
			m.WriteIntoExtraFile("spellLists", spellLists);
		}
		
		m.DeleteObject(name);
	}
}
